package netty.client;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import netty.model.MethodInvokeMeta;

public class ClientChannelHandlerAdapter extends ChannelHandlerAdapter
{
	private MethodInvokeMeta methodInvokeMeta;

	private CustomChannelInitializer channelInitializer;

	public ClientChannelHandlerAdapter(MethodInvokeMeta invokeMeta, CustomChannelInitializer channelInitializer)
	{
		this.methodInvokeMeta = invokeMeta;
		this.channelInitializer = channelInitializer;
	}

	public void channelActive(ChannelHandlerContext ctx)
		throws Exception
	{
		ctx.writeAndFlush(methodInvokeMeta);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
		throws Exception
	{
		cause.printStackTrace();
		ctx.close();
	}

	public void channelRead(ChannelHandlerContext ctx, Object msg)
		throws Exception
	{
		channelInitializer.setResponse(msg);
	}
}
